package com.lzp.controller;

import com.lzp.entity.Teacher;
import com.lzp.service.ITeacherService;
import com.lzp.util.PageResult;
import com.lzp.util.R;
import com.lzp.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;
    //学生列表
    @RequestMapping("/list")
    public R getTeacher(){
        return R.ok().data("list",teacherService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")//分页查询
    public R findByPage(@PathVariable Integer page, @PathVariable Integer pageSize){
        PageResult<Teacher> pr=teacherService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }

    //条件查询带分页
    @RequestMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) TeacherVO vo){
        PageResult<Teacher> pr=teacherService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }

    @PostMapping("/add")
    public R add(@RequestBody Teacher teacher){
        //studentService.add(student);//添加教师
        //return R.ok().message("教师添加成功");
        boolean flag=teacherService.add(teacher);
        return flag?R.ok().message("教师添加成功"):R.error().message("教师添加失败");
    }
    @PutMapping("/update")//修改教师
    public R update(@RequestBody Teacher teacher){
        //studentService.updateById(student);
        //return R.ok().message("教师修改成功");
        boolean flag=teacherService.updateById(teacher);
        return flag?R.ok().message("教师修改成功"):R.error().message("教师修改失败");
    }

    //删除教师
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] split=ids.split(",");//用，进行分割
        List<String> sids= Arrays.asList(split);
        return teacherService.removeByIds(sids)?R.ok().message("删除教师成功!"):R.error().message("删除成功");
    }
}
